package cn.ll.po;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for Task
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see cn.ll.po.Task
 * @author MyEclipse Persistence Tools
 */

public class TaskDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(TaskDAO.class);
	// property constants
	public static final String CONTRACTID = "contractid";
	public static final String CREATEUSER = "createuser";
	public static final String VERIFYUSER = "verifyuser";
	public static final String TYPE = "type";
	public static final String STATUS = "status";
	public static final String PMGRID = "pmgrid";
	public static final String PMGRNAME = "pmgrname";
	public static final String PASTID = "pastid";
	public static final String PASTNAME = "pastname";
	public static final String DEPARTMENTID = "departmentid";
	public static final String ROOTID = "rootid";
	public static final String TASKDETAIL = "taskdetail";

	protected void initDao() {
		// do nothing
	}

	public void save(Task transientInstance) {
		log.debug("saving Task instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public List<Task> selectTaskByTypeAndStatus(int type,int status){
		Session session = this.getSessionFactory().openSession();
		String sql = "select * from task where type ="+type;
		if(status != -1){
			sql += " and status="+status;
		}
		Query query = session.createSQLQuery(sql).addEntity(Task.class);
		List<Task> list = query.list();
		return list;
	}
	
	public void delete(Task persistentInstance) {
		log.debug("deleting Task instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Task findById(String id) {
		log.debug("getting Task instance with id: " + id);
		try {
			Task instance = (Task) getHibernateTemplate().get("cn.ll.po.Task",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Task> findByExample(Task instance) {
		log.debug("finding Task instance by example");
		try {
			List<Task> results = (List<Task>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Task instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Task as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Task> findByContractid(Object contractid) {
		return findByProperty(CONTRACTID, contractid);
	}

	public List<Task> findByCreateuser(Object createuser) {
		return findByProperty(CREATEUSER, createuser);
	}

	public List<Task> findByVerifyuser(Object verifyuser) {
		return findByProperty(VERIFYUSER, verifyuser);
	}

	public List<Task> findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List<Task> findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List<Task> findByPmgrid(Object pmgrid) {
		return findByProperty(PMGRID, pmgrid);
	}

	public List<Task> findByPmgrname(Object pmgrname) {
		return findByProperty(PMGRNAME, pmgrname);
	}

	public List<Task> findByPastid(Object pastid) {
		return findByProperty(PASTID, pastid);
	}

	public List<Task> findByPastname(Object pastname) {
		return findByProperty(PASTNAME, pastname);
	}

	public List<Task> findByDepartmentid(Object departmentid) {
		return findByProperty(DEPARTMENTID, departmentid);
	}

	public List<Task> findByRootid(Object rootid) {
		return findByProperty(ROOTID, rootid);
	}

	public List<Task> findByTaskdetail(Object taskdetail) {
		return findByProperty(TASKDETAIL, taskdetail);
	}
	
	public List findAll() {
		log.debug("finding all Task instances");
		try {
			String queryString = "from Task";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Task merge(Task detachedInstance) {
		log.debug("merging Task instance");
		try {
			Task result = (Task) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Task instance) {
		log.debug("attaching dirty Task instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	/**
	 * 更新task
	 * @param instance
	 */
	public void update(Task instance){
		log.debug("update Task instance");
		try {
			getHibernateTemplate().update(instance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	public void attachClean(Task instance) {
		log.debug("attaching clean Task instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static TaskDAO getFromApplicationContext(ApplicationContext ctx) {
		return (TaskDAO) ctx.getBean("TaskDAO");
	}

	public List getMaxIdTaskByContractId(String contractid) {
		
		log.debug("finding Task instance with property: contractid value: " + contractid);
		try {
			String queryString = "from Task as model where model.contractid = ? order by model.id desc";
			this.getHibernateTemplate().setMaxResults(1);
			List ls = getHibernateTemplate().find(queryString, contractid);
			this.getHibernateTemplate().setMaxResults(-1);
			return ls;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
}